//
//  HRShowRecordView.m
//  Hear
//
//  Created by mac on 2017/4/18.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRShowRecordView.h"
#import "NSMutableAttributedString+GDAttString.h"
@implementation HRShowRecordView

+(void)showRecordView:(RecordBlock)recordBlock andCancelBlock:(CancelBlock)cancelBlock{
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isShowRecordView"];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HRShowRecordView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HRShowRecordView class]]){
        
        
        HRShowRecordView *shareView = (HRShowRecordView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadshowRecordView:recordBlock andCancelBlock:cancelBlock];
        
    }
}

- (void)loadshowRecordView:(RecordBlock)recordBlock andCancelBlock:(CancelBlock)cancelBlock{
    self.recordBlock = recordBlock;
    
    self.cancelBlock = cancelBlock;
    
    UIView *showView = [UIView new];
    showView.layer.cornerRadius = 8;
    showView.clipsToBounds = YES;
    showView.backgroundColor = [UIColor clearColor];
    [self addSubview:showView];
    showView.alpha = 0;
    showView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.3 animations:^{
        showView.alpha = 1.0;
        showView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
    CGFloat showWidth = 260;
    CGFloat showHeight = 350;
    showView.frame = CGRectMake((kFullWidth-showWidth)/2, (kFullHeight-showHeight)/2, showWidth, showHeight);
    
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(20, 40, 230, 200)];
    backView.backgroundColor = CustomColor(240, 240, 240, 1);
    [showView addSubview:backView];
    backView.layer.cornerRadius = 8;
    backView.clipsToBounds = YES;
    
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(backView.left+backView.width/2-40, 0, 80, 80)];
    imageView.image = [UIImage imageNamed:@"红色海豚200像素"];
    [showView addSubview:imageView];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, backView.width-40, 120)];
    contentLabel.attributedText = [NSMutableAttributedString arrtLabelText:@"移除动作是不可撤销的。您确认您不想在名片队列中再听到Ta吗？" arrFont:CustomUIFont(14) attColor:TEXTCOLOR lineLengh:8 alignment:0 colorLenghFrom:0 colorLengh:0];
    contentLabel.textColor = TEXTCOLOR;
    contentLabel.font = CustomUIFont(14);
    [backView addSubview:contentLabel];
    contentLabel.numberOfLines = 0;
//    NSRange range = [contentLabel.text rangeOfString:@"想更好地玩转Hearer？"];
//
//    [HRShowRecordView setTextColor:contentLabel FontNumber:CustomUIFont(11) AndRange:range AndColor:MainColor];
    
    UIButton *enterButton = [[UIButton alloc] initWithFrame:CGRectMake(backView.width/2-90, backView.height-50, 70, 30)];
    [enterButton setTitle:@"确定" forState:UIControlStateNormal];
    [enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    enterButton.titleLabel.font = CustomUIFont(14);
    [enterButton addTarget:self action:@selector(enterButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:enterButton];
    enterButton.backgroundColor = CustomColor(194, 116, 103, 1);
    enterButton.layer.cornerRadius = 8;
    enterButton.clipsToBounds = YES;
    
    
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(backView.width/2+20, enterButton.top, enterButton.width, enterButton.height)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = CustomUIFont(14);
    [cancelButton addTarget:self action:@selector(cancelButton) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cancelButton];
    cancelButton.backgroundColor = [UIColor lightGrayColor];
    
    cancelButton.layer.cornerRadius = 8;
    cancelButton.clipsToBounds = YES;
}


+(void)setTextColor:(UILabel *)label FontNumber:(id)font AndRange:(NSRange)range AndColor:(UIColor *)vaColor
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];
    //设置字号
    [str addAttribute:NSFontAttributeName value:font range:range];
    //设置文字颜色
    [str addAttribute:NSForegroundColorAttributeName value:vaColor range:range];
    
    label.attributedText = str;
}

- (void)enterButtonClick{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].alpha = 0;
        [self.subviews objectAtIndex:0].transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.recordBlock();
    }];
}


- (void)cancelButton{
    self.cancelBlock();
    [self closeView];
}

-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].alpha = 0;
        [self.subviews objectAtIndex:0].transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
